<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>
    
    程序举例（函数） - DSRBLOG
    
  </title>
  

  
  
  <link href="atom.xml" rel="alternate" title="DSRBLOG" type="application/atom+xml">
  <link rel="stylesheet" href="asset/css/style.min.css">
  <link rel="stylesheet" href="asset/css/doc.css">
  <script src="asset/app.js"></script>


</head>

<body> <section class="ct-body">
	<div class="container">
		<div class="columns is-variable bd-klmn-columns is-4 is-centered">
			<div class="column is-four-fifths">
				<div class="post-body single-content">
					
					<h1 class="title">
						程序举例（函数）
					</h1>


					<div class="media">
						
						
						<div class="media-content">
							<div class="content">
								<p>
									<span class="date">2022/01/17 20:41 下午</span>
									

									

									<br />
									<span class="tran-tags">Tags:</span>&nbsp;
									
									<a class="tag is-link is-light">#C语言</a>
									

								</p>
							</div>
						</div>

					</div>
				</div>
				<article class="markdown-body single-content">
					<h2><a id="%E9%A2%98%E7%9B%AE1" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>题目1</h2>
<p>编写函数计算两点之间的距离。要求函数的参数是点的坐标，返回值是两点距离。</p>
<pre class="line-numbers"><code class="language-c">#include &lt;stdio.h&gt;
#include &lt;math.h&gt;

double distance(double p1[2], double p2[2]);

int main()
{
    double p1[2], p2[2];
    printf(&quot;inpuy point 1:&quot;);
    scanf(&quot;%lf,%lf&quot;, &amp;p1[0], &amp;p1[1]);
    printf(&quot;inpuy point 2:&quot;);
    scanf(&quot;%lf,%lf&quot;, &amp;p2[0], &amp;p2[1]);

    printf(&quot;distance is %.2lf\n&quot;, distance(p1, p2));
    return 0;
}

double distance(double p1[2], double p2[2])
{
    return sqrt(
        pow(
            (p1[0] - p2[0]), 2) +
        pow(
            (p1[1] - p2[1]), 2)
    );
}
</code></pre>
<h2><a id="%E9%A2%98%E7%9B%AE2" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>题目2</h2>
<p>使用递归编写函数<code>fun1</code>计算阶乘，并利用<code>fun1</code>计算<code>fun2</code>，函数<code>fun2</code>：<code>n! / m!(n-m)</code></p>
<pre class="line-numbers"><code class="language-c">#include &lt;stdio.h&gt;

int fun1(int n)
{
    if (n == 1)
        return 1;
    else
        return n * fun1(n - 1);
}

double fun2(int n, int m)
{
    return (double)fun1(n) / (fun1(m) * fun1(n - m));
}

int main()
{
    int n, m;
    scanf(&quot;%d %d&quot;, &amp;n, &amp;m);
    printf(&quot;%.2lf&quot;, fun2(n, m));
    return 0;
} 
</code></pre>
<h2><a id="%E9%A2%98%E7%9B%AE3" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>题目3</h2>
<p>在一个字符串<code>s1</code>中查找一子串<code>s2</code>，若存在则返回子串在主串中的起始位置，不存在则返回-1</p>
<p><strong>注意：</strong> 字符串匹配经典思路</p>
<pre class="line-numbers"><code class="language-c">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

int search(char s1[], char s2[])
{
    int i, j;
    int a = strlen(s1);
    int b = strlen(s2);
    i = 0;
    while (i &lt;= a - b) //如果s1[]剩下的字符少于s2[]剩下的字符，内容不匹配，直接跳出循环
    {
        j = 0;
        while (j &lt; b)
        {
            if (s2[j] == s1[i + j]) // i为s1[]当前元素的下标,j为s2[]当前元素的下标,i+j为s2[]当前元素对应的s1[]元素下标
                j++;
            else
                break;
        }
        if (j == b) //如果内层循环正常结束，返回结果
            return i;
        else //如果内层循环由break语句结束，进行下一轮字符匹配
            i++;
    }
    return -1; //如果外层循环结束，表示字符串无匹配，返回-1
}

int main()
{
    char s1[] = &quot;Hello World! This is C!&quot;, s2[] = &quot;ld&quot;;
    printf(&quot;%d&quot;, search(s1, s2));
    return 0;
}
</code></pre>
<h2><a id="%E9%A2%98%E7%9B%AE4" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>题目4</h2>
<p>验证哥德巴赫猜想，任何一个大于2的偶数都可以写成两个素数之和（<strong>哥猜素数：</strong> <em>只能被1和它本身整除，1除外</em>）</p>
<pre class="line-numbers"><code class="language-c">#include &lt;stdio.h&gt;

int isPrime(int n) //验证一个数是否是素数
{
    int i;
    for (i = 2; i &lt; n; i++)
    {
        if (n % i == 0)
            return 0;
    }
    return 1;
}

int main()
{
    int n;
    //验证输入的数字是否符合2&lt;n
    do
    {
        printf(&quot;验证n(2&lt;n,n%%2==0)：&quot;);
        scanf(&quot;%d&quot;, &amp;n);
        printf(&quot;\n&quot;);
    } while (n &lt;= 2);

    //开始验证哥德巴赫猜想
    int i, j;
    for (i = 4; i &lt;= n; i += 2) //外层循环为2&lt;i&lt;=n之间的所有偶数
    {
        printf(&quot;%2d &quot;, i);
        for (j = 2; j &lt;= i / 2; j++) //内层循环验证该偶数(i)是否符合哥德巴赫猜想
        {
            if (isPrime(j) &amp;&amp; isPrime(i - j))
            {
                printf(&quot;%2d %2d\n&quot;, j, i - j);
                break; //若该偶数不符合哥猜，通过break语句强行结束
            }
        }
        if (j == n / 2 + 1) //内层循环正常结束表示该偶数符合哥猜
        {
            printf(&quot;猜想错误&quot;);
        }
    }
    printf(&quot;猜想正确&quot;);
    return 0;
}
</code></pre>

				</article>
				<div class="comments-wrap">
					<div class="share-comments">
						

						

						
					</div>
				</div><!-- end comments wrap -->
			</div>
		</div><!-- end columns -->
	</div><!-- end container -->
</section>  














<script src="asset/prism.js"></script>









</body>

</html>